<script>
import * as util from './common/util'
import { modelGet, modelSet, modelSetWithServer } from './store/modelOpt.js'
import _ from 'lodash'

export default {
  globalData: {
  },
  onLaunch: function (){
    console.log('App Launch')

    // 用户登录
    // #ifdef MP-WEIXIN
    modelSet('loginPromise', this.loginByWeixin())
    // #endif
    // #ifdef H5
    modelSet('loginPromise', this.loginByH5())
    // #endif

  },
  onShow: function (){
    console.log('App Show')
  },
  onHide: function (){
    console.log('App Hide')
  },
  methods: {
    loginByH5(){
      return uniCloud.callFunction({
        name: 'user-center',
        data: {
          action: 'checkToken',
          params: {},
        },
      }).then((res) => {
        if (res.result.err) {
          console.error(res.result.message)
        }
        modelSet('userInfo', res.result.userInfo || {})
      }).catch((e) => {
        console.error(`user-center`, e)
      })
    },
    // 登录1 微信登录
    loginByWeixin(){
      return this.getWeixinCode().then((code) => {
        console.log(`code`, code)
        return uniCloud.callFunction({
          name: 'user-center',
          data: {
            action: 'loginByWeixin',
            params: {
              code,
            },
          },
        })
      }).then((res) => {
        console.log(`loginByWeixin res`, res)// {result:{userInfo:{},}}
        // uni.showModal({
        //   showCancel: false,
        //   content: '微信登录成功',
        // })
        if (res.result.code === 0) {
          this.userInfo = res.result.userInfo
          uni.setStorageSync('uni_id_token', res.result.token)
          uni.setStorageSync('uni_id_token_expired', res.result.tokenExpired)

          modelSet('userInfo', this.userInfo)
          if (!this.userInfo.nickName) { // 没有
            modelSet('homeConfig.modalLoginWxVisible', true)
          }
        }
      }).catch((e) => {
        console.error('loginByWeixin e', e)
        uni.showModal({
          showCancel: false,
          content: '微信登录失败，请稍后再试',
        })
      }).finally(async () => {
        if (this.userInfo._id) {
          // let pageInfoRes = await this.$util.uniDB('juzi-user-page').where({ userId: this.userInfo._id }).get()
          // console.log(`pageInfo`, pageInfoRes)
          // pageNumber = _.get(pageInfoRes, 'result.data[0].pageNumber') || 1
        }
      })
    },

    // 登录2 获取微信code
    getWeixinCode(){
      return new Promise((resolve, reject) => {
        // #ifdef APP-PLUS
        weixinAuthService.authorize(function (res){
          resolve(res.code)
        }, function (err){
          console.log(err)
          reject(new Error('微信登录失败'))
        })
        // #endif
        // #ifdef MP-WEIXIN
        uni.login({
          provider: 'weixin',
          success(res){
            resolve(res.code)
          },
          fail(err){
            reject(new Error('微信登录失败'))
          },
        })
        // #endif
      })
    },
  },
}
</script>

<style>
@import './common/common.css';

/*每个页面公共css */
</style>
